﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;

namespace NathansMapInfoProjects.WindowManager
{
    [Serializable()]
    public class Settings
    {
        /// <summary>
        /// Gets or sets if the application show the window manager form on startup.
        /// </summary>
        public bool AutoLoad { get; set; }

        private double top;
        public double Top
        {
            get { return top; }
            set { top = value; }
        }

        public double Left { get; set; }
        public double Height { get; set; }
        public double Width { get; set; }

        /// <summary>
        /// Gets the path of the settings file that was used
        /// to load the user settings.
        /// </summary>
        [NonSerialized()]
        public string LoadedFrom;

        public static void SaveSettings(Settings settingsFile, string xmlFile)
        {
            XmlSerializer s = new XmlSerializer(typeof(Settings));
            TextWriter w = new StreamWriter(xmlFile);
            s.Serialize(w, settingsFile);
            w.Close();
        }

        public static Settings LoadSettings(string xmlFile)
        {
            if (!File.Exists(xmlFile))
            {
                Settings.SaveSettings(new Settings { AutoLoad = false }, xmlFile);    
            }

            XmlSerializer s = new XmlSerializer(typeof(Settings));
            TextReader r = new StreamReader(xmlFile);
            var settings = (Settings)s.Deserialize(r);
            r.Close();
            settings.LoadedFrom = xmlFile;
            return settings;
        }
    }
}
